package com.yh.utils.extract.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.LocalCacheScope;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态数据源:为源数据源和目标数据源
 * @Title:
 * @Description:
 * @param
 * @return:
 * @throws:
 * @author: maliang
 * @Date: 2019/8/23
 */
@Configuration
@MapperScan(
        basePackages = {"com.yh.**.dynamicDao"},
        sqlSessionFactoryRef = "dynamicSqlSessionFactory"
)
@Slf4j
public class RdcDataSourceConfig {

    @Autowired
    private Environment env;

    @Autowired
    DataSourcePropertes dataSourcePropertes;

    @Autowired
    BeanFactory beanFactory;

    /**
     * 配置数据源
     * @Title:
     * @Description:
     * @param
     * @return:
     * @throws:
     * @author: maliang
     * @Date: 2019/8/2
     */
    @Bean(name = "dynamicDatasource")
    public AbstractRoutingDataSource dynamicDatasource(@Qualifier("defaultDataSource") DataSource defaultDataSource) throws Exception{
        AbstractRoutingDataSource dynamicDatasource=new DynamicDatasource();
        dynamicDatasource.setDefaultTargetDataSource( defaultDataSource);
        Map<Object, Object> sources=new HashMap<>();
        dynamicDatasource.setTargetDataSources(sources);
        return dynamicDatasource;
    }


    @Bean(
            name = {"dynamicSqlSessionFactory"}
    )
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dynamicDatasource") DataSource dynamicDatasource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCacheEnabled(false);
        configuration.setLocalCacheScope(LocalCacheScope.STATEMENT);
        bean.setConfiguration(configuration);
        bean.setDataSource(dynamicDatasource);
        bean.setTypeAliasesPackage("com.yhfin.**.model");
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try {
            bean.setMapperLocations(resolver.getResources("classpath*:com/yh/**/dynamicDao/**/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            log.error("mybatis初始化sqlSessionFactoryBean失败", e);
            return null;
        }
    }

    @Bean(name = "dynamicSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(
            @Qualifier("dynamicSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * 配置动态数据源事务
     * @Title: transactionManager
     * @Description:
     * @param
     * @return:
     * @throws:
     * @author: maliang
     * @Date: 2019/8/2
     */
    @Bean(
            name = {"dynamicTransactionManager"}
    )
    public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDatasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

//    @Bean(name = "dynamicJdbcTemplate")
//    public JdbcTemplate jdbcTemplate(@Qualifier("dynamicDatasource") DataSource dynamicDatasource){
//        JdbcTemplate jdbcTemplate=new JdbcTemplate();
//        jdbcTemplate.setDataSource(dynamicDatasource);
//        return jdbcTemplate;
//    }
}
